package itsm.isperp.module.service.sparepart;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.module.entity.sparepart.SptSparepartInDetail;
import itsm.isperp.module.repository.sparepart.SptSparepartInDetailMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class SptSparepartInDetailService extends
		BaseService<SptSparepartInDetail> {

	@Autowired
	protected SptSparepartInDetailMapper sptSparepartInDetailMapper;

	@Override
	public SptSparepartInDetailMapper getRepository() {
		return this.sptSparepartInDetailMapper;
	}

	public void batchSave(String sparepartInId, String[] sparepartIds) {

		String sql = "INSERT INTO spt_sparepart_in_detail(id,spt_sparepart_in_id,spt_sparepart_id)VALUES(?,?,?)";

		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		List<Object[]> list = new ArrayList<>();

		if (sparepartIds != null && sparepartIds.length > 0) {
			for (String m : sparepartIds) {
				Object[] arg = new Object[3];
				arg[0] = HibernateUUIDGenerator.generate();
				arg[1] = sparepartInId;
				arg[2] = m;

				list.add(arg);
			}

			jdbcTemplate.batchUpdate(sql, list);
		}

	}

	public List<Map<String, Object>> findAllDetailBySparepartInId(
			String sparepartInId) {
		return this.getRepository().findAllDetailBySparepartInId(sparepartInId);
	}

	public void deleteByInId(String sparepartInId) {
		String sql = "DELETE FROM  spt_sparepart_in_detail WHERE spt_sparepart_in_id=?";

		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");

		jdbcTemplate.update(sql, sparepartInId);
	}
}